package com.yunqiao.admin.modules.api.controller;

import com.yunqiao.common.exception.CustomApiException;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.R;
import com.yunqiao.common.validator.ValidatorUtils;
import com.yunqiao.service.api.DistrictPriceService;
import com.yunqiao.service.api.entity.DistrictPriceEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 区域  代理费    定金
 * @author Crocutax
 * @email wangxw725@163.com
 * @date 2019-03-08 17:44:10
 */
@RestController
@RequestMapping("api/districtprice")
public class DistrictPriceApiController {

    @Autowired
    private DistrictPriceService districtPriceService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page =  districtPriceService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
         DistrictPriceEntity districtPrice =  districtPriceService.getById(id);
        return R.ok().put("data",districtPrice);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save( DistrictPriceEntity districtPrice){
        ValidatorUtils.validateEntity( districtPrice);
         districtPriceService.save( districtPrice);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update/{id}")
    public R update(@PathVariable Long id ,  DistrictPriceEntity districtPrice){
         DistrictPriceEntity dbItem =  districtPriceService.getById(id);
        if(dbItem==null){
            throw new CustomApiException("ID有误, 未找到对应数据");
        }
         districtPriceService.updateById( districtPrice);
        return R.ok();
    }




}
